package utils

import (
	"bytes"
	"errors"
	"fmt"
	"k8s-admin/global"
	"net/http"
	"os/exec"
	"runtime"
)

func TestCheck() {
	pingCheck("47.120.69.142")
}

/*站点检测，通过curl去探测对应的url地址*/
func urlCheck(url string) error {
	resp, err := http.Get(url)
	if err != nil {
		global.TPLogger.Error(err)
		return errors.New("探测程序异常")
	} else {
		if resp.StatusCode == 200 {
			return nil
		} else {
			return errors.New(url + "站点访问异常")
		}
	}
}
func pingCheck(IP string) error {
	// 判断一下linux平台还是windows平台，只需要判断是否是windows平台即可，因为windows平台单一
	goos := runtime.GOOS
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	fmt.Println("平台信息：", goos)
	if goos == "windows" {
		fmt.Println("ping", IP)
		cmd := exec.Command("ping", "-n", "4", IP)
		cmd.Stdout = &stdout
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			return errors.New("主机不在线")

		} else {
			return nil
		}
	} else {
		//	linux平台
		cmd := exec.Command("ping", "-c", "4", IP)
		cmd.Stdout = &stdout
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			return errors.New("主机不在线")
		} else {
			return nil
		}
	}
}

func psCheck(ps string) error {
	return nil
}

func postCheck(post int) error {
	return nil
}
